package Common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import Common.CommonDefine;


public class Conf {
	private String confFilePath = "";
	
	private Properties prop;
	
	//Constructor
	public Conf(String confFilePath)
	{
		this.prop = new Properties();
		this.confFilePath = confFilePath;
	}
	
	public int Conf_init()
	{
		try
		{
			prop.load(new FileInputStream(this.confFilePath));
		}
		catch (FileNotFoundException ffe)
		{
			return CommonDefine.EX_FILE_NOT_FOUND_EXCEPTION;
		}
		catch (IOException ioe)
		{
			return CommonDefine.EX_IO_EXCEPTION;
		}
		catch (IllegalArgumentException iae)
		{
			return CommonDefine.EX_ILLEGAL_ARGUEMNT_EXCEPTION;
		}
		catch (Exception e)
		{
			return CommonDefine.EX_UNKNOWN_EXCEPTION;
		}
		
		return 0;
	}
	
	public String Conf_read(String key)
	{
		String tmpl = this.prop.getProperty(key);
		int index = -1;
		
		if (tmpl == null)
			return tmpl;
		
		// comment pass processing.
		if((index = tmpl.indexOf("//")) > -1)
		{
			if(index == 0)
				return null;
			tmpl = tmpl.substring(0, index);
		}
		
		// Left trim processing.
		// http://okjsp.pe.kr/seq/30579
		while((index = tmpl.indexOf(" ")) == 0)
			tmpl.substring(1);
		
		// Right trim processing.
		// http://okjsp.pe.kr/seq/30579
		while((index = tmpl.lastIndexOf(" ")) == (tmpl.length() - 1))
				tmpl = tmpl.substring(0, tmpl.length() - 1);
		return tmpl;
	}
}
